package com.spirng.leetcode.day05;

import java.util.Deque;
import java.util.LinkedList;

public class Test20 {
    public static void main(String[] args) {
        isValid("(]");
    }
    public static boolean isValid(String s) {
        Deque<Character> deque = new LinkedList<>();
        char[] charArray = s.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
           if(charArray[i]=='('){
               deque.addFirst(')');
           } else if (charArray[i]=='[') {
               deque.addFirst(']');
           } else if (charArray[i]=='{') {
               deque.addFirst('}');
           } else if (deque.peekFirst()==null||charArray[i]!=deque.peekFirst()) {
               return false;
           }else {
               deque.removeFirst();
           }
        }
       return deque.size()==0;
    }
}
